package org.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.example.entity.Aircraft;
import org.example.entity.History;
import org.example.exception.MyException;
import org.example.service.AircraftService;
import org.example.service.HistoryService;
import org.example.service.IPubService;
import org.example.service.yy.AttractionsService;
import org.example.utils.Result;
import org.example.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@Api(tags = "历史记录管理")
public class HistoryController {
    @Autowired
    private HistoryService historyService;

    @GetMapping("show-history-list")
    public Result showHistoryList(){
        List<Object> list=new ArrayList<>();
        for (History history : historyService.list(new QueryWrapper<History>().eq("uid",ThreadLocalUtil.getCurrentId()))) {
           history.setData(historyService.showList(history.getClassification(),history.getGood()));
           list.add(history);
        }
        return new Result<>("200","查询成功",list);
    }

    @PostMapping("insert-history")
    @ApiOperation("添加用户最近浏览记录")
    public Result insertHistory(@RequestBody History history){
        history.setUid(Math.toIntExact(ThreadLocalUtil.getCurrentId()));
        history.setTime(new Date().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime());
        if (!historyService.save(history)){
            throw new MyException("444","添加失败");
        }
        return new Result<>("200","添加成功");
    }
}
